package cn.edu.hy.demo06.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Object handler) throws Exception {
        request.setAttribute("pageSize",20);
        System.out.println("loginIn PRe");

        HttpSession session = request.getSession();
        String token = (String) session.getAttribute("token");

        if(token == null || "".equals(token)){
            response.sendRedirect(request.getContextPath()+"/sys/login.do");
        }
        return token!=null;
    }

    @Override
    public void postHandle(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

        request.setAttribute("name","jack");
        System.out.println(request.getAttribute("pageSize"));
        System.out.println("login post");
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }


    @Override
    public void afterCompletion(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("login after");
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
